package com.innjia.app.controller;


import com.innjia.app.annotation.CurrentUser;
import com.innjia.app.annotation.LoginRequired;
import com.innjia.app.entity.ApiUserEntity;
import com.innjia.app.service.ApiNoticeService;
import com.innjia.app.service.ApiUserService;
import com.innjia.base.utils.Result;
import com.innjia.sys.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 类ApiLoginController的功能描述:
 * APP登录授权
 *
 * @auther innjia
 * @date 2017-10-16 14:15:39
 */
@Controller
@RequestMapping("/app/user")
public class ApiUserController {
    @Autowired
    private ApiUserService userApiService;

    @Autowired
    private ApiNoticeService apiNoticeService;

    /**
     * 用户信息
     */

    @RequestMapping(value = "userInfo", method = RequestMethod.POST)
    @LoginRequired
    @ResponseBody
    public Result info(@CurrentUser ApiUserEntity apiUserEntity) {
        ApiUserEntity user = userApiService.userInfo(apiUserEntity.getId());
//        //待办条数
//        int myUpcomingCount = apiActivitiService.myUpcomingCount(apiUserEntity.getId());
//        //我的通知条数
//        int myNoticeCount = apiNoticeService.myNoticeCount(apiUserEntity.getId());
        user.setMyUpcomingCount(0);
        user.setMyNoticeCount(0);
        return Result.ok().put("user", user);
    }

    /**
     * 修改用户信息
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @LoginRequired
    @ResponseBody
    public Result update(UserEntity user) {
        userApiService.update(user);
        return Result.ok();
    }

    /**
     * 修改当前用户密码
     */
    @RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
    @LoginRequired
    @ResponseBody
    public Result updatePassword(UserEntity newUser, @CurrentUser ApiUserEntity apiUserEntity) {
        int i = userApiService.updatePassword(newUser, apiUserEntity);
        if (i < 1) {
            return Result.error("更改密码失败");
        }
        return Result.ok("更改密码成功");
    }


}
